package foo.bar.wiki.web.action.space;

import foo.bar.wiki.web.action.BaseActionBean;
import foo.bar.wiki.web.action.ExtendedPaginatedList;
import foo.bar.wiki.web.action.PaginatedBaseActionBean;
import foo.bar.wiki.web.extension.SpaceTypeConverter;
import foo.bar.wiki.web.extension.UserTypeConverter;
import foo.bar.wiki.domain.Space;
import foo.bar.wiki.domain.User;
import foo.bar.wiki.services.WikiService;
import foo.bar.wiki.services.exception.BusinessLogicExecution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.SimpleMessage;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.integration.spring.SpringBean;

import java.util.Set;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class EditSpaceMembershipActionBean extends PaginatedBaseActionBean {

    public static final String VIEW ="/space/editSpaceMembership.jsp";

    private WikiService wikiService;


    @Validate(converter = SpaceTypeConverter.class)
    private Space space;

    private ExtendedPaginatedList members;
    private ExtendedPaginatedList nonMembers;

    @Validate(converter = UserTypeConverter.class, on = {"addSpaceMembership"})
    private Set<User> toBeMembers;
    @Validate(converter = UserTypeConverter.class, on={"removeSpaceMembership"})
    private Set<User> toBeNonMembers;


    public Resolution view() {
        members = getPaginatedList("spaceMembership_"+space.getId()+"_members");
        nonMembers = getPaginatedList("spaceMembership_"+space.getId()+"_nonMembers");

        members = wikiService.getSpaceMembers(space, members);
        nonMembers = wikiService.getSpaceNonMembers(space, nonMembers);

        return new ForwardResolution(generateThemePath(VIEW));
    }


    public Resolution addSpaceMembership() throws Throwable {
        return handleBusinessLogicException(new BusinessLogicExecution(){
            public Resolution execute() throws Throwable {
                for (User toBeMember : toBeMembers) {
                    if (!space.getMembers().contains(toBeMember)) {
                        space.getMembers().add(toBeMember);
                    }
                }
                wikiService.updateSpace(space);
                getContext().getMessages().add(new SimpleMessage("Memberships added successfully"));
                return new RedirectResolution(EditSpaceMembershipActionBean.class, "view").addParameter("space", space.getId());
            }
        });
    }

    public Resolution removeSpaceMembership() throws Throwable {
        return handleBusinessLogicException(new BusinessLogicExecution(){
            public Resolution execute() throws Throwable {
                for (User toBeNonMember : toBeNonMembers) {
                    if (space.getMembers().contains(toBeNonMember)) {
                        space.getMembers().remove(toBeNonMember);
                    }
                }
                wikiService.updateSpace(space);
                getContext().getMessages().add(new SimpleMessage("Memberships removed successfully"));
                return new RedirectResolution(EditSpaceMembershipActionBean.class, "view").addParameter("space", space.getId());
            }
        });
    }



    @SpringBean("wikiService")
    public void setWikiService(WikiService wikiService) {
        this.wikiService = wikiService;
    }


    public Space getSpace() {
        return space;
    }

    public void setSpace(Space space) {
        this.space = space;
    }

    public ExtendedPaginatedList getMembers() {
        return members;
    }

    public void setMembers(ExtendedPaginatedList members) {
        this.members = members;
    }

    public ExtendedPaginatedList getNonMembers() {
        return nonMembers;
    }

    public void setNonMembers(ExtendedPaginatedList nonMembers) {
        this.nonMembers = nonMembers;
    }


    public Set<User> getToBeMembers() {
        return toBeMembers;
    }

    public void setToBeMembers(Set<User> toBeMembers) {
        this.toBeMembers = toBeMembers;
    }

    public Set<User> getToBeNonMembers() {
        return toBeNonMembers;
    }

    public void setToBeNonMembers(Set<User> toBeNonMembers) {
        this.toBeNonMembers = toBeNonMembers;
    }
}
